package com.wall.springbootinit.controller;

import com.wall.springbootinit.common.BaseResponse;
import com.wall.springbootinit.common.ErrorCode;
import com.wall.springbootinit.common.ResultUtils;
import com.wall.springbootinit.exception.BusinessException;
import com.wall.springbootinit.model.dto.file.UpdateFileDTO;
import com.wall.springbootinit.model.dto.file.UploadByMenuIdDTO;
import com.wall.springbootinit.model.dto.file.UploadFileDTO;
import com.wall.springbootinit.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author: huangmenji
 * @description: TODO
 * @date: 2024/5/19 17:03
 */
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {

    @Resource
    private FileService fileService;

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public BaseResponse<String> upload(UploadFileDTO uploadFileDTO) {
        if (uploadFileDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        MultipartFile file = uploadFileDTO.getFile();
        String filePath = uploadFileDTO.getFilePath();
        String fileDesc = uploadFileDTO.getFileDesc();
        if (file == null || filePath == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long upload = fileService.upload(uploadFileDTO);
        if (upload == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success("上传成功");
    }

    @DeleteMapping("/delete/{id}")
    public BaseResponse<Boolean> delete(@PathVariable Long id) {
        if (id == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean remove = fileService.delete(id);
        if (!remove) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(true);
    }

    @PutMapping("/update")
    public BaseResponse<Boolean> updateFile(@RequestBody UpdateFileDTO updateFileDTO) {
        if (updateFileDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long id = updateFileDTO.getId();
        String newFileName = updateFileDTO.getNewFileName();
        if (id == null || StringUtils.isBlank(newFileName)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean update = fileService.updateFile(updateFileDTO);
        if (!update) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(true);
    }

    @PostMapping("/uploadByMenuId")
    public BaseResponse<Boolean> uploadByMenuId(UploadByMenuIdDTO uploadByMenuIdDTO) {
        if (uploadByMenuIdDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        MultipartFile file = uploadByMenuIdDTO.getFile();
        Long menuId = uploadByMenuIdDTO.getMenuId();
        if (file == null || menuId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean uploadByMenuId = fileService.uploadByMenuId(uploadByMenuIdDTO);
        return ResultUtils.success(uploadByMenuId);
    }
}
